package cn.chengpengper.shortlink.exception;

import cn.chengpengper.shortlink.enums.BizCodeEnum;
import cn.chengpengper.shortlink.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author huchengpeng
 * @version 1.0.0
 * @date 2023/6/18 20:52
 * @description 自定义异常处理器
 */
@Slf4j
@ControllerAdvice
public class CustomExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public JsonData handle(Exception e) {
        if (e instanceof BizException) {
            BizException bizException = ((BizException) e);
            log.error("[业务异常]", e);
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMsg());
        } else {
            log.error("[系统异常]", e);
            return JsonData.buildResult(BizCodeEnum.SYSTEM_ERROR);
        }
    }
}
